import pinyin from './Convert_Pinyin'
var App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cityList: '',
    cityLists: '',
    searchShow: false,
    citytext: '',
    index: '',
    datas: '',


    anxia1: 0,
    anxia2: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    // console.log(option.query);
    let eventChannel = this.getOpenerEventChannel();
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('acceptDataFromOpenerPage', (data) => {
      console.log(data);
      this.setData({
        index: data
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    var that = this;
    wx.request({
      url: 'https://apis.juhe.cn/springTravel/citys?key=' + App.globalData.tokenKey, //仅为示例，并非真实的接口地址
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        // console.log(res.data.result);
        var list = [];
        res.data.result.forEach(element => {
          // console.log(element)
          list = list.concat(element.citys);
        });
        console.log(list)
        that.setData({
          datas: list
        })
        var newList = []
        list.forEach((e, index) => {
          // console.log(e)
          var fullName = pinyin.pinyin.getFullChars(e.city).substr(0, 1);
          if (index == 0) {
            let obj = {
              ZF: fullName,
              citys: [e]
            }
            newList.push(obj);
          } else {
            let i = 0;
            let push = false;
            while (i < newList.length) {
              if (newList[i].ZF == fullName) {
                newList[i].citys = newList[i].citys.concat(e);
                push = true;
              }
              i++;
            };


            if (!push) {
              let objs = {
                ZF: fullName,
                citys: [e]
              }
              let n = 0;
              while (n < newList.length) {
                // console.log(n, fullName, fullName.charCodeAt(), newList[n].ZF, newList[n].ZF.charCodeAt())
                if (fullName.charCodeAt() < newList[n].ZF.charCodeAt()) {
                  // console.log("1111111111");
                  newList.splice(n, 0, objs);
                  break;
                } else if (n == newList.length - 1) {
                  newList.push(objs);
                  break;
                };
                n++;
              };
            }
          }
        })
        console.log(newList);
        that.setData({
          // cityList: res.data.result,
          cityLists: newList,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 点击搜索框查询的城市
  expInput: function (e) {
    console.log(e.detail.value);
    console.log(this.data.cityList)
    this.setData({
      citytext: e.detail.value
    });

    let searchcity = [];
    var patt1 = new RegExp(e.detail.value);
    this.data.datas.forEach((e, index) => {
      console.log(patt1.test(e.city))
      if (patt1.test(e.city)) {
        searchcity.push(e);
      }
    })

    console.log(searchcity)
    this.setData({
      cityList: searchcity,
      searchShow: true,
    })




  },
  // 点击城市获取该城市的id值
  cityid(e) {
    console.log(e.currentTarget.dataset['datas']);

    let eventChannel = this.getOpenerEventChannel()
    // eventChannel.emit('acceptDataFromOpenedPage', { data: 'test' });
    eventChannel.emit('someEvent', {
      data: e.currentTarget.dataset['datas'],
      index: this.data.index
    });
    wx.navigateBack({
      delta: 1
    })
  },

  anxia(e) {
    console.log(e);
    console.log(e.target.dataset);
    this.setData({
      anxia1: e.target.dataset.anxia1 + 1,
      anxia2: e.target.dataset.anxia2 + 1
    })

  },
  move(){
    this.setData({
      anxia1: 0,
      anxia2: 0
    })
  }

})